import 'dart:convert';
import 'dart:io';

import 'package:base_app/commons/constants/app_constant.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uuid/uuid.dart';

class FileUtil {
  static Future<bool> fileExists(String path) async {
    return await File(path).exists();
  }

  static Future<String> fileToBase64(String path) async {
    return await File(path).readAsBytes().then((value) => base64Encode(value));
  }

  static Future<String> setFileFromLocalStorage(filePath) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    String uuid = const Uuid().v1();
    await prefs.setString(uuid, filePath);
    return AppConstant.schemePrefix + uuid;
  }

  static Future<File> getFileFromLocalStorage(String path) async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    var filePath = prefs.getString(path);
    return File(filePath!);
  }
}
